/*调用示例
*创建实例 
*containId是将弹出框作为其子节点的元素id,为空将默认添加到document.body
*popBoxId 是弹出层的Id，默认是popBox
* var popBox=new PopWindow(containId,popBoxId);
*popBox.show(htmlText); //htmlText是弹出框要显示的html片段字符串
*
*/
function PopWindow(containId,popBoxId){
	//用作存储数据
	this.data=[];
	//是否创建过新窗口
	this.hasOpen=false;
	if(containId==document){
		this.contain=document.body;
	}else{
		this.contain=document.getElementById(containId)||document.body;
	}
	this.id=popBoxId||"popBox";
}
//获得视口大小
PopWindow.prototype.getViewport=function(){
	return {
		//IE混杂模式下，须用document.body.clientWidth
		width:document.documentElement.clientWidth||document.body.clientWidth,
		height:document.documentElement.clientHeight||document.body.clientHeight
	}
}
//获得页面大小
PopWindow.prototype.getPagesize=function(){
	return {
		width:Math.max(document.documentElement.scrollWidth,document.documentElement.clientWidth),
		height:Math.max(document.documentElement.scrollHeight,document.documentElement.clientHeight)
	}
}
PopWindow.prototype.setOpacity=function(elem,level){
	elem.style.filter='alpha(opacity='+level+')';//IE滤镜的方法
	elem.style.opacity=level/100;   //W3C
}
PopWindow.prototype.setPos=function(pos){
	this.popBox.style.position=pos;
}
//显示弹出框位置
PopWindow.prototype.showBox=function(){
    //显示遮罩层
	this.pageSize=this.getPagesize();
	this.viewPort=this.getViewport();
	this.hidden.style.width=this.pageSize.width+"px";
	this.hidden.style.height=this.pageSize.height+"px";
	this.hidden.style.display="block";
	//设置在屏幕中的位置
	var that=this;
	this.popBox.style.display="block";
	this.popBox.style.left=(this.viewPort.width-this.popBox.offsetWidth)/2+"px";
	var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;
	var topNum=(this.viewPort.height-this.popBox.offsetHeight)/2+scrollTop;
	if(topNum<0) topNum=0;
	this.popBox.style.top=topNum+"px";
}
PopWindow.prototype.close=function(){
		this.popBox.style.display="none";
		this.hidden.style.display="none";
}
PopWindow.prototype.setHiddenColor=function(color){
	if(this.hidden){
		this.hidden.style.backgroundColor=color;
	}
}
/*
*@param opacity 遮罩透明度 
*/
PopWindow.prototype.init=function(opacity){
	//是否初始化过
		if(this.initFlag!=1){
			this.initFlag=1;
		}else{
			return false;;
		}
		var opacity=opacity||20;
	//将隐藏层添加到文档中
	//创建半透明的遮罩层,并设置样式
		this.hidden=document.createElement("div");
		this.hidden.id="hidden";
		this.hidden.style.backgroundColor="black"
		this.hidden.style.position="absolute";
		this.hidden.style.left="0px";
		this.hidden.style.top="0px";
		this.hidden.style.zIndex=999;
		this.setOpacity(this.hidden,opacity);
		document.body.appendChild(this.hidden);
		this.hidden.style.display="none";
	//添加弹出框，并设定基本样式
		this.popBox=document.createElement("div");
		this.popBox.id=this.id;
		this.contain.appendChild(this.popBox);
		this.popBox.style.position="absolute";
		this.popBox.style.display="none";
		this.popBox.style.zIndex=1000;
		this.popBox.style.position="absolute";
		this.popBox.style.backgroundColor="white";
	/*
		this.closeButton=document.createElement("div");
		this.closeButton.className="closeButton";
		this.closeButton.style.position="absolute";
		this.closeButton.innerHTML="&nbsp;";
		this.popBox.appendChild(this.closeButton);
		var that=this;
		this.closeButton.onclick=function(){
			that.close();
			return false;
		}
	*/
	//添加内容存放容器
		//this.content=document.createElement("div");
		//this.content.className="content";
		//this.popBox.appendChild(this.content);
}
/*
*@param htmlText 添加的html片段
*@param tag  html标签 :div,input
*@param htmlText  tag的innerHTML内容
*/
PopWindow.prototype.addHtml=function(tag,htmlText,className){
	var tag=document.createElement(tag);
	if(className){
		tag.className=className;
	}
	if(htmlText){
		tag.innerHTML=htmlText;
	}
	this.popBox.appendChild(tag);
}
PopWindow.prototype.addScript=function(href){
	var script=document.createElement("script");
	script.src=href;
	document.head.appendChild(script);
}
//显示窗口内容
PopWindow.prototype.show=function(htmlText){
		this.init();
		if(!this.hasOpen){
			this.popBox.innerHTML=htmlText;
			this.hasOpen=true;
		}
		this.showBox();
}
//打开一个新的窗口显示htmlText片段内容
PopWindow.prototype.open=function(htmlText){
		this.init();
		this.hasOpen=true;
		this.popBox.innerHTML=htmlText;
		this.showBox();
}